let { Readable, Writable, Duplex } = require("stream");

let source = ["a", "b", "c"];

// 读写不可通信
class MyDuple extends Duplex {
  constructor(source) {
    super();
    this.source = source;
  }

  // 可读流操作
  _read() {
    let data = this.source.shift() || null;
    this.push(data);
  }

  // 可写流操作
  _write(chunk, enc, next) {
    if (Buffer.isBuffer(chunk)) {
      chunk = chunk.toString();
    }
    process.stdout.write(chunk + "-----");
    process.nextTick(next);
  }
}

let myDuple = new MyDuple(source);

myDuple.on("data", (chunk) => {
  console.log(chunk.toString());
});

myDuple.write("测试数据", "utf-8", () => {
  console.log("双工流测试可写操作");
});
